/* Copyright  2009
   @Author
   Richard Changde Yin            e-mail yinchangde@hotmail.com

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

/**
 * @filename: function/time/from_days.cc
 */
#include "server_includes.h"
#include CSTDINT_H
#include "function/time/from_days.h"
#include "error.h"
#include "temporal.h"
/// std
#include <sstream>
#include <string>

/**
 * Interpret the first argument as a Julian Day Number and fill
 * our supplied temporal object.
 */
bool Item_func_from_days::get_temporal(collapsar::Date &to)
{
  assert(fixed);

  /*
   * We MUST call val_int() before checking null_value because, stupidly,
   * a subselect does not evaluate it's scalar items as null until val_xxx()
   * has been called. :(
   */
  int64_t int_value= args[0]->val_int();

  /* We return NULL from FROM_DAYS() only when supplied a NULL argument */
  if (args[0]->null_value)
  {
    null_value= true;
    return false;
  }

  /* OK, now try to convert from our integer */
  if (! to.from_julian_day_number(int_value))
  {
    /* Bad input, throw an error */
    std::stringstream ss;
    std::string tmp;
    ss << int_value; ss >> tmp;

    my_error(ER_ARGUMENT_OUT_OF_RANGE, MYF(ME_FATALERROR),
            tmp.c_str(), func_name());
    return false;
  }
  return true;
}
